<table class="table table-striped table-bordered table-hover">
    <?php if(empty($couponList)){echo "<i>Hiện không có mã khuyến mãi nào</i>";} else{ ?>
    <thead>
    <tr>
        <th>#</th>
        <th>Tên khuyến mãi</th>
        <th>Mã khuyến mãi</th>
        <th>Ngày hết hạn</th>
        <th>Số lần sử dụng</th>
        <th>Trạng thái</th>
        <th></th>
    </tr>
    </thead>
    <tbody>
    <?php $i = 1; foreach($couponList as $objCouponList){ $myArr = explode(",", $objCouponList->images); ?>
    <tr>
        <td><?php echo $i ?></td>
        <td><a><?php echo $objCouponList->coupon_name ?></a></td>
        <td><?php echo $objCouponList->coupon_code ?></td>
        <td><?php echo $objCouponList->expired_usage ?></td>
        <td><?php echo $objCouponList->usage_time ?></td>
        <td><?php if($objCouponList->coupon_status == 1){echo "Sử dụng";} else{echo "Ngừng sử dụng";} ?></td>
        <td>
            <input type="hidden" id="hiddenEditCouponID<?php echo $i ?>" value="<?php echo $objCouponList->coupon_id ?>">
            <input type="hidden" id="hiddenEditCouponCode<?php echo $i ?>" value="<?php echo $objCouponList->coupon_code ?>">
            <input type="hidden" id="hiddenEditCouponName<?php echo $i ?>" value="<?php echo $objCouponList->coupon_name ?>">
            <input type="hidden" id="hiddenEditDiscountTotal<?php echo $i ?>" value="<?php echo $objCouponList->discount_total ?>">
            <input type="hidden" id="hiddenEditDiscountStyle<?php echo $i ?>" value="<?php echo $objCouponList->discount_style ?>">
            <input type="hidden" id="hiddenEditConditionFrom<?php echo $i ?>" value="<?php echo $objCouponList->condition_from ?>">
            <input type="hidden" id="hiddenEditConditionTo<?php echo $i ?>" value="<?php echo $objCouponList->condition_to ?>">
            <input type="hidden" id="hiddenEditExpiredUsage<?php echo $i ?>" value="<?php echo $objCouponList->expired_usage ?>">
            <input type="hidden" id="hiddenEditMinimumAmount<?php echo $i ?>" value="<?php echo $objCouponList->minimum_amount ?>">
            <input type="hidden" id="hiddenEditUsageTime<?php echo $i ?>" value="<?php echo $objCouponList->usage_time ?>">
            <input type="hidden" id="hiddenEditCouponStatus<?php echo $i ?>" value="<?php echo $objCouponList->coupon_status ?>">
            <input type="hidden" id="hiddenEditDetailApply<?php echo $i ?>" value="<?php echo $objCouponList->detail_apply ?>">
            <input type="hidden" id="hiddenEditDetailStyle<?php echo $i ?>" value="<?php echo $objCouponList->detail_style ?>">

            <a style='cursor: pointer' class='btn btn-warning btn-xs pull-right' onclick="setValueUpdateCoupon($('#hiddenEditCouponID<?php echo $i ?>').val(), $('#hiddenEditCouponCode<?php echo $i ?>').val()
                , $('#hiddenEditCouponName<?php echo $i ?>').val(), $('#hiddenEditDiscountTotal<?php echo $i ?>').val(), $('#hiddenEditDiscountStyle<?php echo $i ?>').val(), $('#hiddenEditConditionFrom<?php echo $i ?>').val()
                , $('#hiddenEditConditionTo<?php echo $i ?>').val(), $('#hiddenEditExpiredUsage<?php echo $i ?>').val(), $('#hiddenEditMinimumAmount<?php echo $i ?>').val()
                , $('#hiddenEditUsageTime<?php echo $i ?>').val(), $('#hiddenEditCouponStatus<?php echo $i ?>').val(), $('#hiddenEditDetailApply<?php echo $i ?>').val(), $('#hiddenEditDetailStyle<?php echo $i ?>').val())">
                <span class='glyphicon glyphicon-pencil'></span>
            </a>
            <a class='btn btn-danger btn-xs pull-right' onclick="deleteCoupon('<?php echo $objCouponList->coupon_id ?>')"><span class='glyphicon glyphicon-remove'></span></a>
        </td>
    </tr>
    <?php $i++; } ?>
    </tbody>
    <?php } ?>
</table>

<!-- input Edit Control Panel -->
<div class="row light" id="lightEditCoupon" style="width: 70%; height: inherit; left: 24%; top: 11%;">
    <div class="panel panel-default">
        <div class="panel-heading">
            <h3 class="panel-title">Chỉnh Sửa Mã Khuyến Mãi</h3>
        </div>
        <div class="panel-body">
            <div class="row" style="margin: 5px;">
                <div class="col-lg-1"><span style="color: red">* </span></div>
                <div class="col-lg-3">Mã Coupon:</div>
                <div class="col-lg-8"><input type="text" class="form-control" id="txtEditCouponCode" disabled></div>
            </div>
            <div class="row" style="margin: 5px;">
                <div class="col-lg-1"><span style="color: red">* </span></div>
                <div class="col-lg-3">Tên Coupon:</div>
                <div class="col-lg-8"><input type="text" class="form-control" id="txtEditCouponName"></div>
            </div>
            <div class="row" style="margin: 5px;">
                <div class="col-lg-1"><span style="color: red">* </span></div>
                <div class="col-lg-3"><span>Giảm:</span></div>
                <div class="col-lg-8">
                    <div class="row">
                        <div class="col-lg-4">
                            <input id="txtEditCouponDiscount" class="form-control" type="text" onkeypress="validate(event);">
                        </div>
                        <div class="col-lg-8">
                            <select class="form-control" id="slEditCouponDiscount">
                                <option value="0">% với mỗi sản phẩm</option>
                                <option value="1">VND với mỗi sản phẩm</option>
                                <option value="2">VND với mỗi đơn hàng</option>
                            </select>
                        </div>
                    </div>
                </div>
            </div>
            <div class="row" style="margin: 5px;">
                <div class="col-lg-1"><span style="color: red">* </span></div>
                <div class="col-lg-3"><span>Điều kiện nhận Coupon (giá trị đơn hàng):</span></div>
                <div class="col-lg-8">
                    <div class="row">
                        <div class="col-lg-1">
                            <span>từ</span>
                        </div>
                        <div class="col-lg-5">
                            <input id="txtEditCouponConditionFrom" class="form-control" type="text" onkeypress="validate(event);">
                        </div>
                        <div class="col-lg-1">
                            <span>đến</span>
                        </div>
                        <div class="col-lg-5">
                            <input id="txtEditCouponConditionTo" class="form-control" type="text" onkeypress="validate(event);">
                        </div>
                    </div>
                </div>
            </div>
            <div class="row" style="margin: 5px;">
                <div class="col-lg-1"><span style="color: red">* </span></div>
                <div class="col-lg-3"><span>Ngày hết hạn:</span></div>
                <div class="col-lg-8"><input id="datetimeEditCouponExpired" class="form-control" type="date"></div>
            </div>
            <div class="row" style="margin: 5px;">
                <div class="col-lg-1"><span style="color: red">* </span></div>
                <div class="col-lg-3"><span>Số tiền tối thiểu của đơn hàng để áp dụng mã coupons:</span></div>
                <div class="col-lg-8"><input id="txtEditCouponMinimumAmount" class="form-control" type="text" value="1" onkeypress="validate(event);"></div>
            </div>
            <div class="row" style="margin: 5px;">
                <div class="col-lg-1"><span style="color: red">* </span></div>
                <div class="col-lg-3"><span>Số lần sử dụng:</span></div>
                <div class="col-lg-8"><input id="txtEditCouponUsageTime" class="form-control" type="text" value="1" onkeypress="validate(event);"></div>
            </div>
            <div class="row" style="margin: 5px;">
                <div class="col-lg-1"><span style="color: red">* </span></div>
                <div class="col-lg-3"><span>Trạng thái:</span></div>
                <div class="col-lg-8">
                    <select class="form-control" id="slEditCouponStatus">
                        <option value="1">Sử dụng</option>
                        <option value="0">Ngừng sử dụng</option>
                    </select>
                </div>
            </div>
            <div class="row" style="margin: 5px;">
                <div class="col-lg-1"><span style="color: red">* </span></div>
                <div class="col-lg-3"><span>Áp dụng:</span></div>
                <div class="col-lg-8" style="padding-left: 30px;">
                    <div class="row">
                        <div class="input-group">
                            <input type="radio" id="radioCategoryApplyEditCoupon" onclick="getEditCategoryNews(0, 0); $('#divCategoryApplyEditCoupon').show(); $('#divChosenCategoryApplyEditCoupon').show(); $('#radioProductApplyEditCoupon').prop('checked', false); $('#divProductApplyEditCoupon').hide(); $('#divSlProductApplyEditCoupon').hide();">
                            <label> &nbsp Áp dụng cho danh mục sản phẩm</label>
                        </div><!-- /input-group -->
                    </div>
                    <div class="row">
                        <div class="col-lg-12" id="divCategoryApplyEditCoupon" style="height: 150px; overflow: auto; padding-top: 5px; margin-bottom: 5px;"></div>
                    </div>
                    <div class="row" style="padding: 10px;" id="divChosenCategoryApplyEditCoupon">
                        <div class="col-lg-6">
                            <span style="text-decoration: underline">Danh mục sản phẩm đã được chọn: </span>
                        </div>
                        <div class="col-lg-6">
                            <input type="hidden" id="hiddenEditCategoryCoupon">
                            <span style="font-weight: bold" id="divSelectedCategoryApplyEditCoupon"></span>
                        </div>
                    </div>
                    <div class="row">
                        <div class="input-group">
                            <input type="radio" id="radioProductApplyEditCoupon" onclick="getEditCategoryNews(0, 1); $('#divProductApplyEditCoupon').show(); $('#divSlProductApplyEditCoupon').show(); $('#radioCategoryApplyEditCoupon').prop('checked', false); $('#divCategoryApplyEditCoupon').hide(); $('#divChosenCategoryApplyEditCoupon').hide();">
                            <label> &nbsp Áp dụng cho sản phẩm</label>
                        </div><!-- /input-group -->
                    </div>
                    <div class="row" id="divProductApplyEditCoupon" style="display: none; height: 150px; overflow: auto; padding-top: 5px; margin-bottom: 5px;">
                        <div class="col-lg-12" id="divProductCategoryApplyEditCoupon"></div>
                    </div>
                    <div class="row" id="divSlProductApplyEditCoupon" style="display: none; margin-top: 20px;">
                        <input type="hidden" id="hiddenEditProductCoupon">
                        <select class="form-control" id="slProductApplyEditCoupon" size="10" style="width: 100%; padding: 10px;" multiple></select>
                    </div>
                </div>
            </div>
            <div class="row" style="text-align: center; margin: 5px">
                <input type="hidden" id="hiddenEditCouponID">
                <input type="button" style="margin: 5px" class="btn btn-success" value="Cập nhật" onclick="editNewCoupon()">
                <input type="button" style="margin: 5px" class="btn btn-success" value="Đóng cửa sổ" onClick="lightbox_close('lightEditCoupon', 'fadeEditCoupon');">
            </div>
        </div>
    </div>
</div>
<div class="row-fluid fade" id="fadeEditCoupon" onClick="lightbox_close('lightEditCoupon', 'fadeEditCoupon');"></div>
<!-- end Edit Control Panel -->

<script>
    function setValueUpdateCoupon(id, code, name, total, diStyle, from, to, expire, amount, time, status, apply, deStyle){
        $('#hiddenEditCouponID').val(id);
        $('#txtEditCouponCode').val(code);
        $('#txtEditCouponName').val(name);
        $('#txtEditCouponDiscount').val(total);

        $('#slEditCouponDiscount').val(diStyle);
        $('#txtEditCouponConditionFrom').val(from);
        $('#txtEditCouponConditionTo').val(to);
        $('#datetimeEditCouponExpired').val(expire);
        $('#txtEditCouponMinimumAmount').val(amount);
        $('#txtEditCouponUsageTime').val(time);
        $('#slEditCouponStatus').val(status);

        if(deStyle == 0){
            $('#hiddenEditCategoryCoupon').val(apply);
            $("#ulProductCoupon").html("");
            var name = getNameProductByID(apply, 1);
            $('#divSelectedCategoryApplyEditCoupon').html("<b>"+name+"<b>");
            getEditCategoryNews(0, 0);
            $('#radioCategoryApplyEditCoupon').click();
        }
        else{
            $('#hiddenEditProductCoupon').val(apply);
            $('#divSelectedCategoryApplyEditCoupon').html("");
            $("#slProductApplyEditCoupon option").remove();
            var myArr = apply.split(",");
            var string = "";
            for(var i = 0; i < myArr.length; i++){
                var name = getNameProductByID(myArr[i], 0);
                $("#slProductApplyEditCoupon").append("<option style='margin: 5px;' value='"+name+"'>"+name+"</option>");
                string += name+",";
            }
            var stringAfter = string.substr(0, string.length-1);
            var myStringArr = stringAfter.split(",");
            $("#slProductApplyEditCoupon").val(myStringArr);
            getEditCategoryNews(0, 1);
            $('#radioProductApplyEditCoupon').click();
        }

        lightbox_open('lightEditCoupon', 'fadeEditCoupon');
    }

    function editNewCoupon(){
        var id = $('#hiddenEditCouponID').val();
        var couponName = $('#txtEditCouponName').val();
        var discountTotal = $('#txtEditCouponDiscount').val();
        var discountStyle = $('#slEditCouponDiscount').val();

        if(couponName == '' || discountTotal == ''){
            alert("Các thông đánh dấu * là các thông tin bắc buộc!");
        }
        else{
            var info = "id="+id+"&couponName="+couponName+"&discountTotal="+discountTotal+"&discountStyle="+discountStyle;
            if(discountStyle == '0'){
                if(discountTotal <= 0 || discountTotal > 100){
                    alert("Tổng số tiền chiết khấu phải lớn hơn 0% và nhỏ hơn hoặc bằng 100%");
                }
                else{
                    checkApplyCouponEdit(info);
                }
            }
            else{
                if(discountTotal < 1){
                    alert("Tổng số tiền chiết khấu phải lớn hơn hoặc bằng 1!")
                }
                else{
                    checkApplyCouponEdit(info);
                }
            }
        }
    }

    function checkApplyCouponEdit(info){
        var conditionFrom = parseFloat($('#txtEditCouponConditionFrom').val());
        var conditionTo = parseFloat($('#txtEditCouponConditionTo').val());
        var expiredUsage = $('#datetimeEditCouponExpired').val();
        var minimumAmount = parseFloat($('#txtEditCouponMinimumAmount').val());
        var usageTime = parseFloat($('#txtEditCouponUsageTime').val());
        var couponStatus = $('#slEditCouponStatus').val();
        var info = info;
        if(conditionFrom < 1 || $('#txtEditCouponConditionFrom').val() == ''){
            alert("Điều kiện giá trị đơn hàng bắt đầu phải lớn hơn hoặc bằng 1!")
        }
        else if(conditionTo <= conditionFrom || $('#txtEditCouponConditionTo').val() == ''){
            alert("Điều kiện giá trị đơn hàng kết thúc phải lớn hơn giá trị đơn hàng bắt đầu!");
        }
        else if(expiredUsage == ''){
            alert("Xin vui lòng nhập ngày hết hạn...");

        }
        else if(minimumAmount < 1 || $('#txtEditCouponMinimumAmount').val() == ''){
            alert("Số tiền tối thiểu phải lớn hơn hoặc bằng 1!");
        }
        else if(usageTime < 1 || $('#txtEditCouponUsageTime').val() == ''){
            alert("Số lần sử dụng phải lớn hơn hoặc bằng 1!");
        }
        else{
            if($('#radioCategoryApplyEditCoupon').is(":checked")){
                var detailApply = $('#hiddenEditCategoryCoupon').val();
                var detailStyle = 0;
                if(detailApply == ''){
                    alert("Xin vui lòng chọn một danh mục bất kỳ...");
                }
                else{
                    var dataString = info+"&conditionFrom="+conditionFrom+"&conditionTo="+conditionTo+"&expiredUsage="+expiredUsage+"&minimumAmount="+minimumAmount+"&usageTime="+usageTime+"&couponStatus="+couponStatus+"&detailApply="+detailApply+"&detailStyle="+detailStyle;
                    updateCoupon(dataString);
                }
            }
            else if($('#radioProductApplyEditCoupon').is(":checked")){
                var detailApply = $('#slProductApplyEditCoupon').val();
                var detailStyle = 1;
                if(detailApply == null){
                    alert("Xin vui lòng chọn ít nhất một sản phẩm...");
                }
                else{
                    var dataString = info+"&conditionFrom="+conditionFrom+"&conditionTo="+conditionTo+"&expiredUsage="+expiredUsage+"&minimumAmount="+minimumAmount+"&usageTime="+usageTime+"&couponStatus="+couponStatus+"&detailApply="+detailApply+"&detailStyle="+detailStyle;
                    updateCoupon(dataString);
                }
            }
        }
    }

    function updateCoupon(dataString){
        var url = "<?php echo base_url() ?>";
        var dataString = dataString;
//        alert(dataString);
        $.ajax({
            type: "POST",
            url: url+"admin_coupon/updateCoupon",
            data: dataString,
            success: function(x){
//                alert(x);
                if(x == 0){
                    alert("Cập nhật thành công!");
                    lightbox_close('lightEditCoupon', 'fadeEditCoupon');
                    getTableCouponManager(1);
                    getPage();
                }
                else{
                    alert(x);
                }
            }
        });
    }

    function getEditCategoryNews(id, isProduct){
        var url = "<?php echo base_url() ?>";
        $.ajax({
            url: url+"admin_coupon/getEditParentCategory/"+id+"/"+isProduct,
            success: function(x){
//                alert(x);
                if(isProduct != 1){
                    $('#divCategoryApplyEditCoupon').html(x);
                }
                else{
                    $('#divProductCategoryApplyEditCoupon').html(x);
                }
            }
        });
    }

    function deleteCoupon(id){
        //alert(id);
        var check = confirm("Bạn chắc chắn muốn xóa?");
        if(check == true){
            var id = id;
            var url = "<?php echo base_url() ?>";
            var dataString = "id="+id;

            $.ajax({
                type: "POST",
                url: url+"admin_coupon/deleteCoupon",
                data: dataString,
                success: function(x){
                    //alert(x);
                    if(x == '0'){
                        getTableCouponManager(1);
                        getPage();
                    }
                    else{
                        alert(x);
                    }
                }
            });
        }
        else{
            return;
        }
    }
</script>

<script>
    function validate(evt) {
        var theEvent = evt || window.event;
        var key = theEvent.keyCode || theEvent.which;
        key = String.fromCharCode( key );
        var regex = /[0-9]/;
        if( !regex.test(key) ) {
            theEvent.returnValue = false;
            if(theEvent.preventDefault) theEvent.preventDefault();
        }
    }

    function getNameProductByID(id, isCate){
//        alert(id);
        var isCate = isCate;
        var id = id;
        var url = "<?php echo base_url() ?>";
        var dataString = "productID="+id;
        var name = '';
        $.ajax({
            async: false,
            type: "POST",
            url: url+"admin_promotion/getNameProductByID/"+isCate,
            data: dataString,
            success: function(x){
//                alert(x);
                name = x;
            }
        });
        return name;
    }
</script>